package cn.csxw.openchina.ui.activity;

import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.OnClick;
import cn.csxw.openchina.R;
import cn.csxw.openchina.base.BaseActivity;
import cn.csxw.openchina.dagger2.component.DaggerNewsDetailActivityComponet;
import cn.csxw.openchina.dagger2.module.NewsDetailActivityModule;
import cn.csxw.openchina.dagger2.presenter.NewsDetailActivityPresenter;
import cn.csxw.openchina.entity.bean.NewsBean;

/**
 * Created by hp on 2017/1/15.
 */

public class NewsDetailActivity extends BaseActivity {
    long exitTime = System.currentTimeMillis();
    @BindView(R.id.tv_news_detail_title)
    TextView mTvNewsDetailTitle;
    @BindView(R.id.tv_news_detail_time)
    TextView mTvNewsDetailTime;
    @BindView(R.id.tv_news_detail_author)
    TextView mTvNewsDetailAuthor;
    @BindView(R.id.wv_detail)
    WebView mWvDetail;
    @BindView(R.id.iv_opt)
    ImageView mIvOpt;
    @BindView(R.id.iv_message_count)
    ImageView mIvMsgCount;
    @BindView(R.id.tv_review_message_count)
    TextView mTvReviewMessageCount;
    @BindView(R.id.iv_write)
    ImageView mIvWrite;
    @BindView(R.id.iv_favor)
    ImageView mIvFavor;
    @BindView(R.id.iv_repost)
    ImageView mIvRepost;
    @BindView(R.id.pb_news_detail)
    ProgressBar mPbNewsDetail;
    @BindView(R.id.iv_write_content)
    ImageButton mIvWriteContent;
    @BindView(R.id.iv_bq)
    ImageView mIvBq;
    @BindView(R.id.iv_send)
    ImageView mIvSend;
    @BindView(R.id.ll_share)
    LinearLayout mLlShare;
    @BindView(R.id.ll_write)
    LinearLayout mLlWrite;
    public NewsBean mNewsBean;
    public NewsBean mHotBean;
    @Inject
    NewsDetailActivityPresenter mNewsDetailActivityPresenter;

    @Override
    public int getLayoutRes() {
        return R.layout.activity_news_detail;
    }

    @Override
    public void initView() {
        mActionBar.setTitle("新闻详情");
        WebSettings webSettings = mWvDetail.getSettings();
        webSettings.setJavaScriptEnabled(true);//支持JS
        webSettings.setAllowFileAccess(true);//允许WebView访问文件数据  
        webSettings.setBuiltInZoomControls(true);//支持内容缩放控制  
    }

    @Override
    public void initListener() {

    }

    @Override
    public void initData() {
        mNewsBean = (NewsBean) getIntent().getSerializableExtra("news");
        mHotBean = (NewsBean) getIntent().getSerializableExtra("hot");
        getNewsData();
    }

    private void getNewsData() {
        DaggerNewsDetailActivityComponet.builder()
                .newsDetailActivityModule(new NewsDetailActivityModule(this))
                .build()
                .inject(this);
        String newsId = "";
        String hotId = "";
        if (mNewsBean != null) {
            newsId = mNewsBean.id;
            mNewsDetailActivityPresenter.callNetWork(newsId);
        }
        if (mHotBean != null) {
            hotId = mHotBean.id;
            mNewsDetailActivityPresenter.callNetWork(hotId);
        }
    }

    @Override
    public void onClick(View v, int id) {

    }

    @OnClick({R.id.wv_detail, R.id.iv_opt, R.id.iv_message_count, R.id.iv_write, R.id.iv_favor, R.id.iv_repost})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.wv_detail:
                break;
            case R.id.iv_write_content:
                Log.d("wgc", "sdsdsadasdasdasdasdasd");
                startWriteAnima();
                break;
            case R.id.iv_opt:
            case R.id.iv_write:
                startShareAnima();
                Log.d("wgc", "我擦");
                break;
            case R.id.iv_message_count:
                break;
            case R.id.iv_favor:
                break;
            case R.id.iv_repost:
                break;
            case R.id.iv_bq:
                break;
            case R.id.iv_send:
                break;
        }
    }

    private void startShareAnima() {
        TranslateAnimation taShare = new TranslateAnimation(0, 0, 0, mLlShare.getHeight());
        taShare.setDuration(200);
        mLlShare.startAnimation(taShare);
        taShare.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                mLlWrite.setVisibility(View.VISIBLE);
                mLlShare.setVisibility(View.GONE);
                TranslateAnimation taWrite = new TranslateAnimation(0, 0, mLlShare.getHeight(),0);
                taWrite.setDuration(200);
                mLlWrite.startAnimation(taWrite);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
    }

    private void startWriteAnima() {

        TranslateAnimation taWrite = new TranslateAnimation(0, 0,0, mLlShare.getHeight());
        taWrite.setDuration(200);
        mLlWrite.startAnimation(taWrite);

        taWrite.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                mLlShare.setVisibility(View.VISIBLE);
                mLlWrite.setVisibility(View.GONE);
                TranslateAnimation taShare = new TranslateAnimation(0, 0, mLlShare.getHeight(), 0);
                taShare.setDuration(200);
                mLlShare.startAnimation(taShare);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });

    }


    public void doNetWork(NewsBean news) {

        mWvDetail.loadUrl(news.url);
        mTvNewsDetailTitle.setText(news.title);
        mTvNewsDetailTime.setText(news.pubDate);
        mTvNewsDetailAuthor.setText(news.author);
        mWvDetail.setWebChromeClient(mWebChromeClient);
    }

    private WebChromeClient mWebChromeClient = new WebChromeClient() {
        @Override
        public void onProgressChanged(WebView view, int newProgress) {
            if( mPbNewsDetail!=null){
                if (newProgress > 20) {
                    mPbNewsDetail.setVisibility(View.GONE);
                } else if (newProgress == 0) {
                    mPbNewsDetail.setVisibility(View.VISIBLE);
                } else {
                    mPbNewsDetail.setVisibility(View.VISIBLE);
                }
            }
        }
    };

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && mWvDetail.canGoBack()) {
            mWvDetail.goBack();
            return true;
        } else {
            if ((System.currentTimeMillis() - exitTime) > 2000) {
                exitTime = System.currentTimeMillis();
            } else {
                finish();
            }
        }
        return true;
    }
}
